package com.tcat.user.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tcat.common.enums.ExceptionEnum;
import com.tcat.common.exception.TcatException;
import com.tcat.common.vo.PageResult;
import com.tcat.user.mapper.PermissionMapper;
import com.tcat.user.pojo.Permissions;
import com.tcat.user.pojo.Roles;
import com.tcat.user.pojo.Users;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.security.Permission;
import java.util.List;
@Service
public class PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    public PageResult<Permissions> queryPermissionsByPage(Integer page, Integer rows, String sortBy, Boolean desc, String search) {
        //分页
        PageHelper.startPage(page,rows);
        /*
           WHERE 'name' like '%x%' OR letter =='x'
           ORDER BY id DESC
         */
        //过滤
        Example example=new Example(Permissions.class);
        if(StringUtils.isNotBlank(search)){
            example.createCriteria().orLike("name","%"+search+"%");
                   // .orEqualTo("letter",search.toUpperCase());
        }
        //排序
        if(StringUtils.isNotBlank(sortBy)){
            String orderByClause=sortBy +(desc ? " DESC":" ASC");
            example.setOrderByClause(orderByClause);
        }
        //查询
        //分页助手会拦截查询并对他进行分页
        Page<Permissions> list = (Page<Permissions>)permissionMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            throw new TcatException(ExceptionEnum.PERMISSION_NOT_FOUND);
        }
        //解析分页结果
        PageInfo<Permissions> info=new PageInfo<>(list);
        return new PageResult<>(info.getTotal(),list);
    }



    public void deleteByPermissionInRps(Long pid) {
        permissionMapper.deleteByPermissionsInRps(pid);
    }

    /**
     * 删除品牌  单个（多个删除则每个遍历每个品牌进行删除）
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deletePermissions(long id) {
        permissionMapper.deleteByPrimaryKey(id);
        deleteByPermissionInRps(id);
    }


    /**
     * 根据角色id查询权限 用于修改角色信息时，权限信息的回显
     * @param rid
     * @return
     */
    public List<Permissions> queryByRoleId(Long rid){

        return this.permissionMapper.queryByRoleId(rid);
    }

    /**
     * 新增权限
     * @param permission
     */
    @Transactional(rollbackFor = Exception.class)
    public void savePermissions(Permissions permission) {
        //新增品牌
        permission.setId(null);
        int count = permissionMapper.insert(permission);
        if(count!=1){
            throw new TcatException(ExceptionEnum.PERMISSION_SAVE_ERROR);
        }

    }

    /**
     * 权限更新
     * @param permission
     */
    @Transactional(rollbackFor=Exception.class)
    public void updatePermission(Permissions permission){
        this.permissionMapper.updateByPrimaryKeySelective(permission);
    }


    /**
     * 查询所有权限
     * @return
     */
    public List<Permissions> queryAll(){
        return permissionMapper.selectAll();
    }
}
